<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="500px" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="100px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('策略名称')" prop="name">
          <el-input v-emoji v-model="record.name" style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('动作')" prop="action">
          <el-select size="small" v-model="record.action" style="width:217px;">
            <el-option v-for="(item, index) in actionList" :key="index" :label="item" :value="index">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('日志使能')" prop="log">
          <el-select size="small" v-model="record.log" style="width:217px;">
            <el-option v-for="(item, index) in logList" :key="index" :label="item" :value="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{$t('确 定')}}</el-button>
      <el-button @click="close">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { autoAddPolicy } from '@/api/safe/intellect/learnServer'

export default {
  name: 'ServerDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        action: 'ACCEPT',
        log: 'on',
        rule_map: ''
      },
      recordCopy: {
        name: '',
        action: 'ACCEPT',
        log: 'on',
        rule_map: ''
      },
      actionList: {
        'ACCEPT': this.$t('允许'),
        'DROP': this.$t('丢弃')
      },
      logList: {
        'on': this.$t('开'),
        'off': this.$t('关')
      },
      protocolList: {},
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (groupStr, selectList, protocolList, data) {
      this.dialogVisible = true
      this.protocolList = {}
      for (let item of protocolList) {
        this.protocolList[item.PROTOCOL_NUM] = item.PROTOCOL_NAME + '_' + item.PROTOCOL_NUM
      }
      let strList = JSON.parse(JSON.stringify(groupStr))
      let seList = JSON.parse(JSON.stringify(selectList))
      let ruleMap = []
      for (let item of strList) {
        if (seList[item]) {
          let itemList = item.split(' | ')
          let str = {
            'L4_PROTOCOL': this.protocolList[seList[item][0].protocol],
            'ipType': seList[item][0].ipType,
            'DESTINATION_IP': itemList[0],
            'SOURCE_IP': '',
            'DESTINATION_PORT': itemList[1]
          }
          for (let itemIpPort of seList[item]) {
            if (str.SOURCE_IP.indexOf(itemIpPort.name) < 0) {
              str.SOURCE_IP += itemIpPort.name + ','
            }
            // if (str.DESTINATION_PORT.indexOf(itemIpPort.DESTINATION_PORT) < 0) {
            //   str.DESTINATION_PORT += itemIpPort.DESTINATION_PORT + ','
            // }
          }
          // 去除尾部逗号
          str.SOURCE_IP = str.SOURCE_IP.replace(/(\s|,)+$/g, '')
          // str.DESTINATION_PORT = str.DESTINATION_PORT.replace(/(\s|,)+$/g, '')
          ruleMap.push(str)
        }
      }
      this.record.rule_map = JSON.stringify(ruleMap)
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const rule_map = JSON.parse(this.record.rule_map);
          for(const item of rule_map) {
            if(['组播地址', '广播地址','Multicast address', 'Broadcast address'].includes(item.ipType)) {
              item.ipType ='ipv4';
            }
          }
          this.record.rule_map = JSON.stringify(rule_map);
          autoAddPolicy(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('执行成功'),
                type: 'success'
              })
              this.$emit('dialogReVisible', res.info)
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = JSON.parse(JSON.stringify(this.recordCopy))
      this.$refs.form.resetFields()
    }
  }
}
</script>
